import requests
import time
import csv

# 截至到2021年1月23日的数据
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36'
}
# 获取全部年的数据
urlyear = 'https://api.huobipro.com/market/history/kline?period=1year&size=2000&symbol=xrpusdt&AccessKeyId=3290265c-208217e4-9f4199c1-mk0lklo0de'
# 获取全部月的数据
urlmon = 'https://api.huobipro.com/market/history/kline?period=1mon&size=2000&symbol=xrpusdt&AccessKeyId=3290265c-208217e4-9f4199c1-mk0lklo0de'
# 获取全部周的数据
urlweek = 'https://api.huobipro.com/market/history/kline?period=1week&size=2000&symbol=xrpusdt&AccessKeyId=3290265c-208217e4-9f4199c1-mk0lklo0de'
# 获取全部天的数据
urlday = 'https://api.huobipro.com/market/history/kline?period=1day&size=2000&symbol=xrpusdt&AccessKeyId=3290265c-208217e4-9f4199c1-mk0lklo0de'
# 获取全部小时的数据
urlhour = 'https://api.huobipro.com/market/history/kline?period=60min&size=2000&symbol=xrpusdt&AccessKeyId=3290265c-208217e4-9f4199c1-mk0lklo0de'
# response = requests.get(urlyear, headers=headers)
# response = requests.get(urlmon, headers=headers)
# response = requests.get(urlweek, headers=headers)
response = requests.get(urlday, headers=headers)
print(response.status_code)
# print(response.json())
if response.json().get("status") == "ok":
    datas = response.json().get("data")
    # 输出data全部的数据
    print(datas)
    #界面我们再取每条数据

    # with open(r"C:\Users\PC\Desktop\火币\瑞波币\年统计.csv", 'w', encoding='GB2312', newline="") as f:
    # with open(r"C:\Users\PC\Desktop\火币\瑞波币\月统计.csv", 'w', encoding='GB2312', newline="") as f:
    # with open(r"C:\Users\PC\Desktop\火币\瑞波币\周统计.csv", 'w', encoding='GB2312', newline="") as f:
    with open(r"C:\Users\PC\Desktop\火币\瑞波币\天统计.csv", 'w', encoding='GB2312', newline="") as f:
        writer = csv.writer(f)
        writer.writerow(['k线id', '成交量', '成交笔数', '开盘价', '收盘价', '最低价', '最高价', '成交额'])
        for data in datas:
            print(data)
            id = data['id']
            amount = data['amount']
            count = data['count']
            open = data['open']
            close = data['close']
            low = data['low']
            high = data['high']
            vol = data['vol']
            writer.writerow([id, amount, count, open, close, low, high, vol])
f.close()